import 'package:dk_flutter_base/dk_core.dart';
import 'package:dk_flutter_base/dk_ui.dart';
import 'package:dk_flutter_module_sale/common/config/page_base_common.dart';
import 'package:dk_flutter_module_sale/common/plugin/bean/bean_preview_video.dart';
import 'package:dk_flutter_module_sale/common/plugin/plugin_common.dart';
import 'package:dk_flutter_module_sale/common/user_track/user_track.dart';
import 'package:dk_flutter_module_sale/config/route/sale_check_widget_route.dart';
import 'package:dk_flutter_module_sale/sale/api/sale_check_api.dart';
import 'package:dk_flutter_module_sale/sale/sale_check/room_paint/bean/image_bean.dart';
import 'package:dk_flutter_module_sale/sale/sale_check/room_paint/bean/request_bean.dart';
import 'package:dk_flutter_module_sale/sale/sale_check/room_paint/widget/image_widget.dart';
import 'package:dk_flutter_module_sale/sale/sale_check/room_paint/widget/section_widget.dart';

@ARoute(url: SaleCheckWidgetRoute.SALE_CHECK_ROOM_PAINT_DETAIL)
//房态采集
class PageRoomPaintDetail extends CommonPageWithTrack {
  final BoostRouteOption option;
  PageRoomPaintDetail(this.option) : super();

  @override
  void trackStatistics() {
    UserTrack.trackViewScreen("巡房-房态采集-详情", null);
  }

  @override
  void onInitState(CommonPageBloc pageBloc, {Map<String, Bloc> extraBlocMap}) {
    RequestBean requestBean = RequestBean.fromJson(this.option.params);
    pageBloc.dataHttpGet(
        showLoading: false,
        path: SaleCheckApi.roomPaintDetail,
        queryMap: requestBean.toJson(),
        handleResult: (dynamic data) {
          AreaDataBean dataBean = AreaDataBean.fromJson(data);
          pageBloc.switchPage(RoomPaintStatefulWidget(pageBloc,requestBean,dataBean));
        });
  }
}

class RoomPaintStatefulWidget extends StatefulWidget {
  final RequestBean paramBean;
  final CommonPageBloc pageBloc;
  final AreaDataBean dataBean;
  RoomPaintStatefulWidget(this.pageBloc, this.paramBean,this.dataBean);
  @override
  _RoomPaintStatefulWidgetState createState() =>
      _RoomPaintStatefulWidgetState(pageBloc, this.paramBean,this.dataBean);
}

class _RoomPaintStatefulWidgetState extends State<RoomPaintStatefulWidget> {
  final RequestBean paramBean;
  final CommonPageBloc pageBloc;
  DkCenterDialog tipDialog;
  AreaDataBean dataBean;
  CommonPlugin commonPlugin = CommonPlugin();
  _RoomPaintStatefulWidgetState(this.pageBloc, this.paramBean,this.dataBean);

  void refresh({bool showLoading = false}){
    pageBloc.dataHttpGet(
        showLoading: showLoading,
        path: SaleCheckApi.roomPaintDetail,
        queryMap: this.paramBean.toJson(),
        handleResult: (dynamic data) {
          setState(() {
            this.dataBean = AreaDataBean.fromJson(data);
          });
        });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: DkAppBar(DKAppBarParams.withBackMenu(
          context, buildRightNavigationItem(),
          title: Text("房态采集"))),
      backgroundColor: Colors.white,
      body: SafeArea(
        bottom: true,
        child: Stack(
          children: <Widget>[
            Container(
              color: Colors.white,
              child: CustomScrollView(
                physics: ScrollPhysics(),
                slivers: getSliverToBoxAdapters(),
              ),
            ),
          ],
        ),
      ),
    );
  }

  List<Widget> buildRightNavigationItem() {
    return [GestureDetector(
      onTap: (){
        RouteUtil.goPage(SaleCheckWidgetRoute.SALE_CHECK_ROOM_PAINT_EDIT +
            "?suiteId=${this.paramBean.suiteId}",widgetContext:context,widgetResultCallback: (map){
          this.refresh();
        });
      },
      child: Padding(
        padding: EdgeInsets.only(left: 15, right: 10),
        child: Center(
          child: DkText.simple(
            "编辑",
            16,
            DkColor("0xFF999999"),
          ),
        ),
      ),
    )];
  }

  List<Widget> getSliverToBoxAdapters() {
    List<Widget> boxAdapters = List<Widget>();
    if (this.dataBean == null) return boxAdapters;
    Widget gap = SliverToBoxAdapter(
        child: Container(
      color: Color(0xFFF7F8FA),
      height: 10,
    ));
    //外部和公共视频
    for (int i = 0; i < this.dataBean.areas.length; i++) {
      AreaBean areaBean = this.dataBean.areas[i];
      if (areaBean.enType == RoomEnvironmentType.RoomEnvironmentTypeOut &&
          areaBean.url.length > 0) {
        boxAdapters.add(SliverToBoxAdapter(
            child: SectionWidget(
          title: areaBean.areaName,
          bottomLineHidden: false,
        )));
        boxAdapters.addAll(videoArea(areaBean));
        boxAdapters.add(gap);
      } else if (areaBean.enType ==
              RoomEnvironmentType.RoomEnvironmentTypeCommon &&
          areaBean.url.length > 0) {
        boxAdapters.add(SliverToBoxAdapter(
            child: SectionWidget(
          title: areaBean.areaName,
          bottomLineHidden: false,
        )));
        boxAdapters.addAll(videoArea(areaBean));
        boxAdapters.add(gap);
      }
    }
    SliverToBoxAdapter separateLine = SliverToBoxAdapter(
      child: Divider(
        height: 0.5,
        indent: 15,
        endIndent: 15,
        color: Color(0xFFEBEBEB),
      ),
    );
    //房间内部视频
    boxAdapters.add(SliverToBoxAdapter(
        child: SectionWidget(
      title: "房间内部视频",
      bottomLineHidden: false,
    )));
    for (int i = 0; i < this.dataBean.areas.length; i++) {
      AreaBean areaBean = this.dataBean.areas[i];
      if (areaBean.enType == RoomEnvironmentType.RoomEnvironmentTypeInner) {
        boxAdapters.add(SliverToBoxAdapter(
            child: SectionWidget(
          titleStyle: TextStyle(
              fontSize: 14,
              color: Color(0xFF666666),
              fontWeight: FontWeight.w600),
          title: areaBean.areaName + ":" + areaBean.roomCode,
        )));
        boxAdapters.addAll(videoArea(areaBean));
        if (i != this.dataBean.areas.length - 1) {
          boxAdapters.add(separateLine);
        }
      }
    }
    return boxAdapters;
  }

  List<Widget> videoArea(AreaBean areaBean) {
    List<Widget> boxAdapters = List<Widget>();
    int count = areaBean.url.length;
    EdgeInsets padding;
    if (areaBean.enType == RoomEnvironmentType.RoomEnvironmentTypeInner) {
      padding = EdgeInsets.only(left: 10, right: 10, top: 0, bottom: 0);
    } else {
      padding = EdgeInsets.only(left: 10, right: 10, top: 17, bottom: 17);
    }
    SliverPadding sliverPadding = SliverPadding(
      padding: padding,
      sliver: new SliverGrid(
        //Grid
        gridDelegate: new SliverGridDelegateWithFixedCrossAxisCount(
          crossAxisCount: 4,
          mainAxisSpacing: 0,
          crossAxisSpacing: 0.0,
          childAspectRatio: 1.0,
        ),
        delegate: new SliverChildBuilderDelegate(
          (BuildContext context, int index) {
            Url url = areaBean.url[index];
            return ImageWidget(
              tapAction: () {
                PreviewVideoBean previewBean =
                    PreviewVideoBean(videoFilePath: url.videoUrl);
                this.commonPlugin.previewVideo(bean: previewBean);
              },
              imageURL: url.imageUrl,
            );
          },
          childCount: count,
        ),
      ),
    );
    boxAdapters.add(sliverPadding);
    return boxAdapters;
  }
}
